# -*- coding: UTF-8 -*-
import re
import time
import pathlib

# 过滤号码
def numberfilter(number):
    # 过滤规则 如需减少过滤规则可以注释或者直接删除
    # 注释直接在前面加上# 如： # {'code': '包含6A', 'reg': r'.+(\d)\1{5,}'},
    # 需要还原只需把#删除
    regs = [
        {'code': '包含6A', 'reg': r'.+(\d)\1{5,}'},
        {'code': '包含7A', 'reg': r'.+(\d)\1{6,}'},
        {'code': '包含8A', 'reg': r'.+(\d)\1{7,}'},
        {'code': '包含AAAA', 'reg': r'.+(\d)\1{3,}'},
        {'code': '包含AAABBB', 'reg': r'.+(\d)\1{2,5}?(\d)\2{2,5}?'},
        {'code': '包含ABABAB', 'reg': r'.+(\d)((?!\1)\d)\1\2\1\2'},
        {'code': '尾AAA', 'reg': r'.+(\d)\1{2,}$'},
        {'code': '尾AAAB', 'reg': r'.+(\d)\1{2,5}?(\d)$'},
        {'code': '尾AAABA', 'reg': r'.+(\d)\1\1(\d)\1$'},
        {'code': '尾AAABAAAB', 'reg': r'.+(\d)\1\1(\d)\1\1\1\2$'},
        {'code': '尾AAABBB', 'reg': r'.+(\d)\1{2,5}?(\d)\2{2,5}?$'},
        {'code': '尾AABAA', 'reg': r'.+(\d)\1(\d)\1\1$'},
        {'code': '尾AABB', 'reg': r'\d{6}([0-9])(?!1)([0-9])\2(?!2)([0-9])\3'},
        {'code': '尾AABBB', 'reg': r'\d{6}([0-9])\1{1}(?!1)([0-9])\2{2}'},
        {'code': '尾AABBBB', 'reg': r'\d{5}([0-9])\1{1}(?!1)([0-9])\2{3}'},
        {'code': '尾AABBBBB', 'reg': r'\d{4}([0-9])\1{1}(?!1)([0-9])\2{4}'},
        {'code': '尾AABBCC', 'reg': r'\d{4}([0-9])(?!1)([0-9])\2(?!2)([0-9])\3(?!3)([0-9])\4'},
        {'code': '尾AABBCCC', 'reg': r'\d{3}([0-9])(?!1)([0-9])\2{1}(?!2)([0-9])\3{1}(?!3)([0-9])\4{2}'},
        {'code': '尾AABBCCCC', 'reg': r'\d{2}([0-9])(?!1)([0-9])\2{1}(?!2)([0-9])\3{1}(?!3)([0-9])\4{3}'},
        {'code': '尾AABBCCDD', 'reg': r'\d{3}([0-9])\1([0-9])\2([0-9])\3([0-9])\4'},
        {'code': '尾ABABABAB', 'reg': r'\d{3}(\d)((?!\1)\d)\1\2\1\2\1\2'},
        {'code': '尾ABABCCC', 'reg': r'.+(\d)(?!1)(\d)\1\2(\d)\3{2}$'},
        {'code': '尾ABABCCCC', 'reg': r'.+(\d)(?!1)(\d)\1\2(\d)\3{3}$'},
        {'code': '尾ABC', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}\d$'},
        {'code': '尾ABCD', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3}\d$'},
        {'code': '尾ABCABC', 'reg': r'\d{5}(\d{3})\1$'},
        {'code': '尾ABCDABCD', 'reg': r'\d{3}(\d{4})\1$'},
        {'code': '尾ABCDE', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){4}\d$'},
        {'code': '尾ABCDEF', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){5}\d$'},
        {'code': '尾1349任意组合', 'reg': r'^\d{7}(1|3|4|9)(1|3|4|9)(1|3|4|9)(1|3|4|9)'},
        {'code': '尾2678任意组合', 'reg': r'^\d{7}(2|6|7|8)(2|6|7|8)(2|6|7|8)(2|6|7|8)'},
        {'code': '尾正3顺', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){2}\d$'},
        {'code': '尾倒3顺', 'reg': r'.+(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){2}\d$'},
        {'code': '尾正4顺', 'reg': r'.+(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3}\d$'},
        {'code': '尾倒4顺', 'reg': r'.+(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3}\d$'},
        {'code': '尾520', 'reg': r'^\d{8}(520)'},
        {'code': '尾号8且最后4位不包含4', 'reg':r'^\d{7}(0|1|2|3|5|6|7|8|9){3}(8)'},
        {'code': '生日号1960-2021', 'reg': r'\d{3}(19[6-9]\d|20[0-2][0-1])'},
        {'code': '尾ABAB', 'reg': r'^\d{7}(\d)(?!\1)(\d)\1\2'},
        {'code': '中间正4顺', 'reg': r'^\d{3}(?:0(?=1)|1(?=2)|2(?=3)|3(?=4)|4(?=5)|5(?=6)|6(?=7)|7(?=8)|8(?=9)){3}\d\d{4}'},
        {'code': '中间倒4顺', 'reg': r'^\d{3}(?:9(?=8)|8(?=7)|7(?=6)|6(?=5)|5(?=4)|4(?=3)|3(?=2)|2(?=1)|1(?=0)){3}\d\d{4}'},
        {'code': '包含AABBCCDD', 'reg': r'\d([0-9])\1([0-9])\2([0-9])\3([0-9])\4'},
    ]
    for reg in regs:  # 循环查询并将输出到result文件夹里
        regex = reg['reg']
        pattern = re.compile(regex)
        if pattern.match(number):
            i = 0
            path = pathlib.Path("result/"+reg['code'] + ".txt")
            if path.is_file() == False:
                file = open("result/"+reg['code'] + ".txt", 'w')
                file.close()
            else:
                with open("result/"+reg['code'] + ".txt", 'r') as f:
                    rows = f.readlines()
                    for nbr in rows:
                        try:
                            nbr = nbr.strip('\n')  # 去掉读取中的换行字符
                            if nbr == number:
                                i = i + 1
                        except:
                            continue
                if i == 0:
                    f = open("result/" + reg['code'] + ".txt", "a+")
                    f.writelines(number + "\n")
                    f.close()
                    print(number, reg['code'])
        else:
            i = 0
            path = pathlib.Path("result/普通号码.txt")
            if path.is_file() == False:
                file = open("result/普通号码.txt", 'w')
                file.close()
            else:
                with open("result/普通号码.txt", 'r') as f:
                    rows = f.readlines()
                    for nbr in rows:
                        try:
                            nbr = nbr.strip('\n')  # 去掉读取中的换行字符
                            if nbr == number:
                                i = i+1
                        except:
                            continue
                if i == 0:
                    f = open("result/普通号码.txt", "a+")
                    f.writelines(number + "\n")
                    f.close()
                    print(number, '普通号码')



# 入口 请在专业人士指导下修改，如修改后出现问题概不负责
if __name__ == '__main__':
    start = time.time()
    filepath = 'data/number.txt'
    path = pathlib.Path(filepath)
    if path.is_file():
        with open(filepath, 'r') as f:
            rows = f.readlines()
            for number in rows:
                try:
                    number = number.strip('\n')  # 去掉读取中的换行字符
                    numberfilter(number)
                except:
                    continue
    else:
        print('文件不存在！请将文件重命名number.txt放在data下')
    end = time.time()
    print("程序结束用时:{}分钟".format((int(end - start)/60)))

